local skel = fk.CreateSkill {
  name = "emo__mosi",
  dynamic_desc = function (self, player, lang)
    if player:hasSkill("#rumo", true) then
      return "emo__mosi_rumo"
    end
  end,
}

Fk:loadTranslationTable{
  ["emo__mosi"] = "魔思",
  [":emo__mosi"] = "每当你受到伤害后，你可以摸X张牌（X为你本轮未使用或打出的花色数），摸到的【杀】无次数限制，若均为【杀】则你<a href='#RuMoDesc'><font color='red'>入魔</font></a>，改成造成伤害后发动。",

  [":emo__mosi_rumo"] = "每当你造成伤害后，你可以摸X张牌（X为你本轮未使用或打出的花色数），摸到的【杀】无次数限制。每轮结束时，若你本轮未造成伤害，失去一点体力。",
  ["#RuMoDesc"] = "入魔是每局游戏限一次的操作，入魔后，每轮结束时，若本轮你未造成过伤害，你失去1点体力。",

  ["@emo__mosi-round"] = "魔思",
  ["@@emo__mosi-inhand"] = "魔思",
}

---@param player ServerPlayer
local on_use = function (self, event, target, player, data)
  local cards = player:drawCards(4 - #player:getTableMark("@emo__mosi-round"), skel.name)
  for _, id in ipairs(cards) do
    if table.contains(player.player_cards[Player.Hand], id) and Fk:getCardById(id).trueName == "slash" then
      player.room:setCardMark(Fk:getCardById(id), "@@emo__mosi-inhand", 1)
    end
  end
  if #cards > 0 and table.every(cards, function (id) return Fk:getCardById(id).trueName == "slash" end) then
    if Fk.skills["#rumo"] and not player:hasSkill("#rumo", true) then
      player.room:notifySkillInvoked(player, skel.name, "big")
      player.room:handleAddLoseSkills(player, "#rumo", nil, false, true)
    end
  end
end

skel:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player and #player:getTableMark("@emo__mosi-round") < 4 then
      return not player:hasSkill("#rumo", true)
    end
  end,
  on_use = on_use,
})

skel:addEffect(fk.Damage, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player and #player:getTableMark("@emo__mosi-round") < 4 then
      return player:hasSkill("#rumo", true)
    end
  end,
  on_use = on_use,
})

skel:addEffect(fk.CardUsing, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name, true) and data.card.suit ~= Card.NoSuit
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addTableMarkIfNeed(player, "@emo__mosi-round", data.card:getSuitString(true))
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@emo__mosi-round", 0)
end)

skel:addAcquireEffect(function (self, player, is_start)
  local suits = {}
  player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
    local use = e.data
    if use.from == player and use.card.suit ~= Card.NoSuit then
      table.insertIfNeed(suits, use.card:getSuitString(true))
    end
  end, Player.HistoryRound)
  if #suits > 0 then
    player.room:setPlayerMark(player, "@emo__mosi-round", suits)
  end
end)

skel:addEffect("targetmod", {
  bypass_times = function (self, player, skill, scope, card, to)
    return card and card:getMark("@@emo__mosi-inhand") ~= 0
  end,
})

skel:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return target == player and data.card:getMark("@@emo__mosi-inhand") ~= 0 and data.card.trueName == "slash"
  end,
  on_refresh = function (self, event, target, player, data)
    data.extraUse = true
  end,
})

return skel
